#include <windows.h>
//#include "resource.h"
//#include <CommCtrl.h>

#include <AppModel/Include/AppLogic.h>

#include <TorusProRenderingApp/WinApp.h>
#include <TorusProRenderingApp/WinWindow.h>

#include <vld.h>

AppModel::AppLogic* app_logic;

// -------------------------------------------------------------------------

/*
LRESULT CALLBACK setup_dialog_handler(HWND hDlg,UINT wm,WPARAM wp,LPARAM lp)
{
	static HWND hlist;
	switch(wm)
	{
	case WM_INITDIALOG:
		{
            SendDlgItemMessage(hDlg,IDC_OPTION_DX9,BM_SETCHECK,BST_CHECKED,0);
            
            SendDlgItemMessage(hDlg, IDC_SLIDER1, TBM_SETRANGE, (WPARAM)1, (LPARAM)MAKELONG(10, 50));

            SendDlgItemMessage(hDlg, IDC_SLIDER1, TBM_SETTICFREQ, 1, 5);

            SendDlgItemMessage(hDlg, IDC_SLIDER1, TBM_SETPOS, 1, 12);

            SendDlgItemMessage(hDlg, IDC_SLIDER2, TBM_SETRANGE, (WPARAM)1, (LPARAM)MAKELONG(1, 10));

            SendDlgItemMessage(hDlg, IDC_SLIDER2, TBM_SETTICFREQ, 1, 2);

            SendDlgItemMessage(hDlg, IDC_SLIDER2, TBM_SETPOS, 1, 5);
			return true;
		}
		break;
	case WM_COMMAND:
		switch(wp)
		{
			case IDOK:
			{				
				int checked;
				// ventana
				checked = SendDlgItemMessage(hDlg,IDC_OPTION_DX9,BM_GETCHECK,0,0);
				if( checked == BST_CHECKED )
				{
					rendering_platform_chosen = GameLogic::RenderingPlatform::RENDER_WITH_DX9;
				}

				checked = SendDlgItemMessage(hDlg,IDC_OPTION_DX11,BM_GETCHECK,0,0);
				if( checked == BST_CHECKED )
				{
					rendering_platform_chosen = GameLogic::RenderingPlatform::RENDER_WITH_DX11;
                }

                scene_size = SendDlgItemMessage(hDlg, IDC_SLIDER1, TBM_GETPOS, (WPARAM)0, (LPARAM)0);
                snake_velocity = SendDlgItemMessage(hDlg, IDC_SLIDER2, TBM_GETPOS, (WPARAM)0, (LPARAM)0);

				EndDialog(hDlg,true);
				return 1;
			}
			break;
			case IDCANCEL:
			{
				EndDialog(hDlg,false);
				MessageBox(hDlg,L"Oh! You have chosen to quit this game!",L"Quit From Game",MB_ICONEXCLAMATION);
				return 1;
			}
			break;            
		}		
		break;
	}
	
	return 0;
};
*/

//---------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg,
                             WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,
				   LPSTR lpCmdLine, int nCmdShow)
{
    /*
    DLGPROC dlgproc;
    dlgproc=(DLGPROC)setup_dialog_handler;
    bool goon = (bool)DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,dlgproc);

    if( !goon)
    {
        return 0;
    }
    */
    MSG   Msg;
	LPCTSTR ClsName = L"Win32OOP";
	LPCTSTR WndName = L"Torus PRO Rendering";

	// Initialize the application class
	WinApp* winApp = new WinApp(hInstance, ClsName, MainWndProc);
	winApp->Register();


	// Create the main window
	int win_width = 500;
	int win_height = 500;
	WinWindow* wnd = new WinWindow();
	wnd->Create(hInstance, ClsName, WndName, NULL, WS_OVERLAPPEDWINDOW , 0, 0, 0, win_width, win_height);
	
    // Display the main window
	wnd->Show();
	
    app_logic = new AppModel::AppLogic(wnd, AppModel::AppLogic::RenderingPlatform::RENDER_WITH_DX9);
    app_logic->run();
 	// Process the main window's messages
	while( GetMessage(&Msg, NULL, 0, 0) )
	{
        TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
    
    app_logic->stop();
    delete app_logic;

	delete winApp;
	delete wnd;

	return 0;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch(Msg)
	{
    case WM_CLOSE:
	case WM_DESTROY:
        {
            PostQuitMessage(WM_QUIT);
        }
		return 0;
        break;
    case WM_KEYDOWN:
        {
            AppModel::AppLogic::AppInputType input_type;
            switch (wParam)
            {
            case VK_RIGHT:
                input_type = AppModel::AppLogic::AppInputType::RIGHT_ARROW_PRESSED;
                break;
            case VK_LEFT:
                input_type = AppModel::AppLogic::AppInputType::LEFT_ARROW_PRESSED;
                break;
            case VK_UP:
                input_type = AppModel::AppLogic::AppInputType::UP_ARROW_PRESSED;
                break;
            case VK_DOWN:
                input_type = AppModel::AppLogic::AppInputType::DOWN_ARROW_PRESSED;
                break; 
            case VK_SHIFT:
                input_type = AppModel::AppLogic::AppInputType::SHIFT_KEY_PRESSED;
                break;
            case VK_CONTROL:
                input_type = AppModel::AppLogic::AppInputType::CONTROL_KEY_PRESSED;
                break;
            case VK_SPACE:
                input_type = AppModel::AppLogic::AppInputType::SPACE_BAR_PRESSED;
                break;
           }            
           app_logic->input(input_type);
        }
        break;    
	}

    
	return DefWindowProc(hWnd, Msg, wParam, lParam);
}
//-----------------------------------------------




